package com.slkj.shilixiaoyuanapp.view.choosedialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SimpleItemAnimator;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.slkj.shilixiaoyuanapp.R;
import com.umeng.commonsdk.debug.I;

import java.util.ArrayList;
import java.util.List;

public class ChooseDialog<T> extends AlertDialog implements View.OnClickListener {
    private Context context;
    //是否为单选。false多选，true单选
    private boolean isRadio;
    private List<T> list = new ArrayList<>();
    private List<T> backData = new ArrayList<>();
    private List<ChooseBean> chooseBeans = new ArrayList<>();
    private RecyclerView recyclerView;
    private TextView cancel,sure;
    private ChooseAdapter adapter;
    private OnSureClickListener listener;

    public void setListener(OnSureClickListener<T> listener) {
        this.listener = listener;
    }

    public ChooseDialog(@NonNull Context context, List<T> list) {
        super(context, R.style.normal_dialog);
        this.context = context;
        this.list = list;
        this.isRadio = false;
    }

    public ChooseDialog(@NonNull Context context, List<T> list,boolean isRadio) {
        super(context, R.style.normal_dialog);
        this.context = context;
        this.list = list;
        this.isRadio = isRadio;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);//状态栏透明
        getWindow().setBackgroundDrawableResource(R.drawable.selector_choose_dialog_corner);
        setContentView(R.layout.dialog_choose_list);
        initWidget();
        init();
    }

    private void initWidget() {
        recyclerView = findViewById(R.id.dialog_choose_recycler);
        cancel = findViewById(R.id.dialog_choose_cancel);
        sure = findViewById(R.id.dialog_choose_sure);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        ((SimpleItemAnimator)recyclerView.getItemAnimator()).setSupportsChangeAnimations(false);

        //dialog基本设置
        setCanceledOnTouchOutside(true);
        cancel.setOnClickListener(this);
        sure.setOnClickListener(this);
    }

    private void init() {
        for (int i = 0; i < list.size(); i++) {
            ChooseData chooseData = (ChooseData) list.get(i);
            chooseBeans.add(new ChooseBean(chooseData.getChooseDataText()));
        }
        adapter = new ChooseAdapter(chooseBeans,context);
        recyclerView.setAdapter(adapter);
    }


    @Override
    public void onClick(View v) {
        if (v == cancel){
            //取消
            dismiss();
        }
        if (v == sure){
            //确定，返回一个位置集合
            if (listener != null) {
                backData.clear();
                for (int i = 0; i < chooseBeans.size(); i++) {
                    if (chooseBeans.get(i).isChoose()){
                        backData.add(list.get(i));
                    }
                }
                listener.onSureClick(backData);
            }
            dismiss();
        }
    }

    @Override
    public void dismiss() {
        super.dismiss();

    }

    @Override
    public void show() {
        try {
            super.show();
        } catch (Exception e) {

        }
    }

    public interface OnSureClickListener<T> {
        void onSureClick(List<T> list);
    }
}
